    //
//  RCCostDetailVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-7-2.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCCostDetailVC.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "GloableEngineService.h"
#import "LVBaseTableView.h"
#import "IManager+Balance.h"
#import "RCConsumeCell.h"
#import "RCMerchantDetailVC.h"
#import "NSArray+EX.h"

@interface RCCostDetailVC ()<UITableViewDataSource,UITableViewDelegate,IManagerDelegate>{
    BOOL _refresh;
    RCCostListDataModel *_dataModel;
    NSInteger _pn;
}
@property (nonatomic, retain) RCCostListDataModel *dataModel;
@property (nonatomic,retain) MBProgressHUDEx *progressHub;
@property (nonatomic,assign) int64_t requestID;
@end

@implementation RCCostDetailVC
@synthesize dataModel = _dataModel;
@synthesize progressHub = _progressHub;
@synthesize requestID = _requestID;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) dealloc
{
    [_dataModel release];
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.titleLabelText = @"我的订单";
    self.navigationItem.titleView = [self createTitleView];
    
    UILabel *nickName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    nickName.text = [[GloableEngineService sharedInstance] nickName];
    [nickName setFont:[UIFont fontWithName:kFontNameLightHeiti size:15]];
    [nickName setTextColor:[UIColor colorWithHex:0xff505050]];
    [nickName setTextAlignment:NSTextAlignmentCenter];
    [nickName setBackgroundColor:[UIColor colorWithHex:0xfff4f4f4]];
    [self.viewContainer addSubview:nickName];
    [nickName release];
    
    
    // 设置tableview相关参数
    if ( baseVCTableView != nil ) {
        self.view.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        baseVCTableView.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        baseVCTableView.backgroundColor = [UIColor clearColor];
        baseVCTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        baseVCTableView.frame = self.view.bounds;
        CGRect tableViewNewFrame = CGRectMake(0, 50, 320, UIScreenHeight - 44- 25);
        baseVCTableView.frame = tableViewNewFrame;
        baseVCTableView.tableHeaderView = nil;
        baseVCTableView.tableFooterView = nil;
        [self addPullRefresh];
    }
    

    [self refreshData];

}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance]cancelFetchDataRequestByRequestID:_requestID];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) loadMoreData
{
    _refresh = NO;
    
    NSString *page = [NSString stringWithFormat:@"%d",_pn];
    _requestID = [[IManager sharedInstance] requestCostDetail:[[GloableEngineService sharedInstance] bduss] forPage:page withDelegate:self];
    
}

#pragma mark
- (void) refreshData
{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    _refresh = YES;
    _pn = 0;
    _requestID = [[IManager sharedInstance] requestCostDetail:[[GloableEngineService sharedInstance] bduss] forPage:@"0" withDelegate:self];
}


#pragma mark 
- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if (_requestID == requestId) {
        if (_refresh) {
            [_progressHub removeFromSuperview];
            [_progressHub release];
            _progressHub = nil;
            [self finishRefreshingData];
            _refresh = NO;
        }else{
            [self finishLoadMoreData];
        }
    }
    
}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    
    if (_requestID == requestId) {
        RCCostListDataModel *costList = (RCCostListDataModel *)dataModel;
        if (_refresh) {
            [_progressHub removeFromSuperview];
            [_progressHub release];
            _progressHub = nil;
            self.dataModel = costList;
            
//            [baseVCTableView reloadData];
            [self finishRefreshingData];
            _refresh = NO;
        }else{
            RCCostListDataModel *newData = (RCCostListDataModel *)dataModel;
            self.dataModel.pn = newData.pn;
            self.dataModel.rn = newData.rn;
            self.dataModel.total = newData.total;
            [self.dataModel.listItems addObjectsFromArray:costList.listItems];
            
            [baseVCTableView reloadData];
            [self finishLoadMoreData];
        }
        
        
        
        NSInteger currentTotal = [self.dataModel.listItems count];
        _pn = currentTotal;
        
        if (currentTotal < self.dataModel.total) {
            [self addPullLoadMore];
        }else{
            [self noMoreData];
        }
    }

}

#pragma mark

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataModel.listItems count];;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CostCellIdentifier";
    RCConsumeCell *cell = (RCConsumeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (nil == cell) {
        cell = [[[RCConsumeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]autorelease];
    }
    [cell bindData:[[self.dataModel listItems] safeObjectAtIndex:[indexPath row]]];
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RCCostItemDataModel *detailItem = [self.dataModel.listItems safeObjectAtIndex:[indexPath row]];
    return [RCConsumeCell getCellHeight:detailItem];
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    
}
@end
